

package com.haoxi.gateway.smgpmsg;


import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;

public class SMGPForwardMessage extends SMGPMessage
{

    private StringBuffer strBuf;

    public SMGPForwardMessage(byte msgId[], String destSMGWNo, String srcSMGWNo, String smcNo, int msgType, int reportFlag, int priority, 
            String serviceId, String feeType, String feeCode, String fixedFee, int msgFormat, Date validTime, Date atTime, 
            String srcTermId, String destTermId, String chargeTermId, String msgContent, String reserve)
        throws IllegalArgumentException
    {
        if(msgId == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:MsgID value is null.");
        if(msgId.length > 10)
            throw new IllegalArgumentException("The parameter of forword message is wrong:MsgID length is larger than 10");
        if(destSMGWNo == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:DestSMGWNo value is null.");
        if(destSMGWNo.length() > 6)
            throw new IllegalArgumentException("The parameter of forword message is wrong:DestSMGWNo length is larger than 6");
        if(srcSMGWNo == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:SrcSMGWNo value is null."); 
        if(srcSMGWNo.length() > 6)
            throw new IllegalArgumentException("The parameter of forword message is wrong:SrcSMGWNo length is larger than 6");
        if(smcNo == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:SMCNo value is null.");
        if(smcNo.length() > 6)
            throw new IllegalArgumentException("The parameter of forword message is wrong:SMCNo length is larger than 6");
        if(msgType < 0 || msgType > 255)
            throw new IllegalArgumentException("The parameter of forword message is wrong:MsgType less than 0 or larger than 255.");
        if(reportFlag < 0 || reportFlag > 255)
            throw new IllegalArgumentException("The parameter of forword message is wrong:ReportFlag less than 0 or larger than 255.");  
        if(priority < 0 || priority > 9)
            throw new IllegalArgumentException("The parameter of forword message is wrong:Priority less than 0 or larger than 9.");
        if(serviceId == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:ServiceID value is null.");
        if(serviceId.length() > 10)
            throw new IllegalArgumentException("The parameter of forword message is wrong:ServiceID length is larger than 10");
        if(feeType == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:FeeType value is null.");
        if(feeType.length() > 2)
            throw new IllegalArgumentException("The parameter of forword message is wrong:FeeType length is larger than 2");
        if(feeCode == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:FeeCode value is null.");
        if(feeCode.length() > 6)
            throw new IllegalArgumentException("The parameter of forword message is wrong:FeeCode length is larger than 6");
            
        if(fixedFee == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:FixedFee value is null.");
        if(fixedFee.length() > 6)
            throw new IllegalArgumentException("The parameter of forword message is wrong:FixedFee length is larger than 6");
        if(msgFormat < 0 || msgFormat > 255)
            throw new IllegalArgumentException("The parameter of forword message is wrong:MsgFormat less than 0 or larger than 255.");
        if(srcTermId == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:SrcTermID value is null.");
            
        if(srcTermId.length() > 21)
            throw new IllegalArgumentException("The parameter of forword message is wrong:SrcTermID length is larger than 21");
            
        if(destTermId == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:DestTermId value is null.");
            
        if(destTermId.length() > 21)
            throw new IllegalArgumentException("The parameter of forword message is wrong:DestTermId length is larger than 21");
        if(chargeTermId == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:ChargeTermID value is null.");
            
        if(chargeTermId.length() > 21)
            throw new IllegalArgumentException("The parameter of forword message is wrong:ChargeTermID length is larger than 21");
            
        if(msgContent == null)
            throw new IllegalArgumentException("The parameter of forword message is wrong:MsgContent value is null.");
            
        if(msgContent.length() > 252)
            throw new IllegalArgumentException("The parameter of forword message is wrong:MsgContent length is larger than 252");
            
        if(reserve != null && reserve.length() > 8)
            throw new IllegalArgumentException("The parameter of forword message is wrong:Reserve length is larger than 8");
            
        int len = 174 + msgContent.length();
        buf = new byte[len];
        TypeConvert.int2byte(len, buf, 0);
        TypeConvert.int2byte(5, buf, 4);
        System.arraycopy(msgId, 0, buf, 12, 10);
        System.arraycopy(destSMGWNo.getBytes(), 0, buf, 22, destSMGWNo.length());
        System.arraycopy(srcSMGWNo.getBytes(), 0, buf, 28, srcSMGWNo.length());
        System.arraycopy(smcNo.getBytes(), 0, buf, 34, smcNo.length());
        buf[40] = (byte)msgType;
        buf[41] = (byte)reportFlag;
        buf[42] = (byte)priority;
        System.arraycopy(serviceId.getBytes(), 0, buf, 43, serviceId.length());
        System.arraycopy(feeType.getBytes(), 0, buf, 53, feeType.length());
        System.arraycopy(feeCode.getBytes(), 0, buf, 55, feeCode.length());
        System.arraycopy(fixedFee.getBytes(), 0, buf, 61, fixedFee.length());
        buf[67] = (byte)msgFormat;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        if(validTime != null)
        {
            String tmpTime = String.valueOf(String.valueOf(dateFormat.format(validTime))).concat("032+");
            System.arraycopy(tmpTime.getBytes(), 0, buf, 68, 16);
        }
        if(atTime != null)
        {
            String tmpTime = String.valueOf(String.valueOf(dateFormat.format(atTime))).concat("032+");
            System.arraycopy(tmpTime.getBytes(), 0, buf, 85, 16);
        }
        System.arraycopy(srcTermId.getBytes(), 0, buf, 102, srcTermId.length());
        System.arraycopy(destTermId.getBytes(), 0, buf, 123, destTermId.length());
        System.arraycopy(chargeTermId.getBytes(), 0, buf, 144, chargeTermId.length());
        buf[165] = (byte)msgContent.length();
        System.arraycopy(msgContent.getBytes(), 0, buf, 166, msgContent.length());
        if(reserve != null)
            System.arraycopy(reserve.getBytes(), 0, buf, 166 + msgContent.length(), reserve.length());
        strBuf = new StringBuffer(600);
        strBuf.append(",DestSMGWNo=".concat(String.valueOf(String.valueOf(destSMGWNo))));
        strBuf.append(",SrcSMGWNo=".concat(String.valueOf(String.valueOf(srcSMGWNo))));
        strBuf.append(",SMCNo=".concat(String.valueOf(String.valueOf(smcNo))));
        strBuf.append(",MsgType=".concat(String.valueOf(String.valueOf(msgType))));
        strBuf.append(",ReportFlag=".concat(String.valueOf(String.valueOf(reportFlag))));
        strBuf.append(",Priority=".concat(String.valueOf(String.valueOf(priority))));
        strBuf.append(",ServiceID=".concat(String.valueOf(String.valueOf(serviceId))));
        strBuf.append(",FeeType=".concat(String.valueOf(String.valueOf(feeType))));
        strBuf.append(",FeeCode=".concat(String.valueOf(String.valueOf(feeCode))));
        strBuf.append(",fixedFee=".concat(String.valueOf(String.valueOf(fixedFee))));
        strBuf.append(",MsgFormat=".concat(String.valueOf(String.valueOf(msgFormat))));
        if(validTime != null)
            strBuf.append(",ValidTime=".concat(String.valueOf(String.valueOf(dateFormat.format(validTime)))));
        else
            strBuf.append(",ValidTime=null");
        if(atTime != null)
            strBuf.append(",AtTime=".concat(String.valueOf(String.valueOf(dateFormat.format(atTime)))));
        else
            strBuf.append(",at_Time=null");
        strBuf.append(",SrcTermID=".concat(String.valueOf(String.valueOf(srcTermId))));
        strBuf.append(",DestTermID=".concat(String.valueOf(String.valueOf(destTermId))));
        strBuf.append(",ChargeTermID=".concat(String.valueOf(String.valueOf(chargeTermId))));
        strBuf.append(",MsgLength=".concat(String.valueOf(String.valueOf(msgContent.length()))));
        strBuf.append(",MsgContent=".concat(String.valueOf(String.valueOf(msgContent))));
        strBuf.append(",Reserve=".concat(String.valueOf(String.valueOf(reserve))));
    }

    public String toString()
    {
        StringBuffer outBuf = new StringBuffer(600);
        outBuf.append("SMGPForwardMessage: ");
        outBuf.append("PacketLength=".concat(String.valueOf(String.valueOf(buf.length))));
        outBuf.append(",RequestID=5");
        outBuf.append(",SequenceID=".concat(String.valueOf(String.valueOf(getSequenceId()))));
        if(strBuf != null)
            outBuf.append(strBuf.toString());
        return outBuf.toString();
    }

    public int getRequestId()
    {
        return SMGPCommon.SMGP_FORWARD;
    }
}
